﻿using System.Data.Entity;
using ITTestingSystem.Domain.Entities;

namespace ITTestingSystem.Domain
{
    /// <summary>
    /// Specifies the database context
    /// </summary>
    /// 
    public class DomainContext : DbContext
    {
        /// <summary>
        /// Creates a new database context
        /// </summary>
        /// 
        public DomainContext()
            : base("PrimaryConnectionString")
        { }

        /// <summary>
        /// Encapsulates the table for the users entity
        /// </summary>
        /// 
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// Encapsulates the table for the roles entity
        /// </summary>
        /// 
        public DbSet<Role> Roles { get; set; }

        /// <summary>
        /// Encapsulates the table for the tests entity
        /// </summary>
        /// 
        public DbSet<Test> Tests { get; set; }

        /// <summary>
        /// Encapsulates the table for the questions entity
        /// </summary>
        /// 
        public DbSet<Question> Questions { get; set; }

        /// <summary>
        /// Encapsulates the table for the questions type entity
        /// </summary>
        /// 
        public DbSet<QuestionType> QuestionTypes { get; set; }

        /// <summary>
        /// Encapsulates the table for the answers entity
        /// </summary>
        /// 
        public DbSet<Answer> Answers { get; set; }

        /// <summary>
        /// Encapsulates the table for the authentications detail entity
        /// </summary>
        /// 
        public DbSet<AuthenticationsDetail> AuthenticationsDetails { get; set; }

        /// <summary>
        /// Encapsulates the table for the categories entity
        /// </summary>
        /// 
        public DbSet<Category> Categories { get; set; }

        /// <summary>
        /// Encapsulates the table for the completed tests detail entity
        /// </summary>
        /// 
        public DbSet<CompletedTestsDetail> CompletedTestsDetails { get; set; }

        /// <summary>
        /// Encapsulates the table for the tests description entity
        /// </summary>
        /// 
        public DbSet<TestsDescription> TestsDescriptions { get; set; }

        /// <summary>
        /// Encapsulates the table for the tests statistic entity
        /// </summary>
        /// 
        public DbSet<TestsStatistic> TestsStatistics { get; set; }

        /// <summary>
        /// Encapsulates the table for the tests topic entity
        /// </summary>
        /// 
        public DbSet<TestsTopic> TestsTopics { get; set; }

        /// <summary>
        /// Returns a <see cref="T:System.Data.Entity.DbSet`1"/> instance for 
        /// access to entities of the given type in the context
        /// and the underlying store
        /// </summary>
        /// 
        /// <param name="modelBuilder">
        /// The builder that defines the model for the context being created
        /// </param>
        /// 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // When both sides of the connection 1 - 1 is required 
            // EF can not determine which party is the principal, and is dependent 
            // You must specify clearly

            modelBuilder.Entity<User>().HasRequired(user => user.AuthenticationsDetail)
                                       .WithRequiredPrincipal(detail => detail.User);

            modelBuilder.Entity<Test>().HasRequired(test => test.TestsDescription)
                                       .WithRequiredPrincipal(description => description.Test);

            modelBuilder.Entity<Test>().HasRequired(test => test.TestsStatistic)
                                       .WithRequiredPrincipal(statistic => statistic.Test);
        }
    }
}
